﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using MyList_Enumerator;

namespace MyList_Enumerator
{
    class MyListTEnum_out<T> : IEnumerator<T> where T : IComparable
    {
        /// <summary>
        /// Constructor of the file
        /// </summary>
        /// <param name="myList"></param>
        public MyListTEnum_out(MyList<T> list)
        {
            this.myList = list;
            position = -1;
            positionStart = 0;
            positionEnd = list.Count();
            index = 0;
        }


        #region Inserted 'MyListTEnum_out<T>' class

        public T Current
        {
            get { return current; }
        }

        public void Dispose()
        {
        }

        object IEnumerator.Current
        {
            get
            {
                try
                {
                    current = myList.GetPos(position);
                    return current;
                }
                catch (IndexOutOfRangeException)
                {
                    throw new InvalidOperationException();
                }
            }
        }

        public bool MoveNext()
        {
            if (index++ % 2 == 0)
            {
                position = positionStart++;
            }
            else
            {
                position = positionEnd--;
            }
            
            return (positionEnd >= positionStart);
        }

        public void Reset()
        {
            position = -1;
            positionStart = -1;
            positionEnd = myList.Count();
        }
        
            #endregion

            #region Private fields

            private T current;
            private int position;
            private int positionStart;
            private int positionEnd;
            private int index;
            private MyList<T> myList;

            #endregion
    }
}
